package com.hans.myscanner.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;


import com.haha.extend.button.StateBitmapImageButton;
import com.hans.dialog.CustomDialog;
import com.hans.myscanner.R;
import com.hans.myscanner.entity.ContactEntity;
import com.hans.myscanner.entity.RecordEntity;
import com.hans.myscanner.widget.CustomFontTextView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/6/15.
 */
public class HistoryRecordAdapter extends BaseAdapter {
    private Context context;
    private List<RecordEntity> recordEntityList;

    public HistoryRecordAdapter(Context context, List<RecordEntity> recordEntityList) {
        this.context = context;
        this.recordEntityList = recordEntityList;
    }

    @Override
    public int getCount() {
        return recordEntityList.size();
    }

    @Override
    public Object getItem(int position) {
        return recordEntityList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if(convertView==null){
        convertView=View.inflate(context, R.layout.history_listview_item,null);
            viewHolder=new ViewHolder();
            viewHolder.recordTypeIcon= (ImageView) convertView.findViewById(R.id.record_type_icon);
            viewHolder.recordContent= (CustomFontTextView) convertView.findViewById(R.id.record_content);
            viewHolder.recordFrom= (CustomFontTextView) convertView.findViewById(R.id.record_from);
            viewHolder.recordTime= (CustomFontTextView) convertView.findViewById(R.id.record_time);
            viewHolder.deleteBtn=(StateBitmapImageButton)convertView.findViewById(R.id.delete_btn);
            convertView.setTag(viewHolder);
        }else
        {
            viewHolder=(ViewHolder) convertView.getTag();
        }

        final RecordEntity entity= (RecordEntity) getItem(position);
        RecordEntity.RECORD_TYPE recordType=entity.getRecordType();


        int[] recordTypeDrawables={R.mipmap.tmb_contact,R.mipmap.tmb_url,R.mipmap.tmb_text};
        viewHolder.recordTypeIcon.setImageResource(recordTypeDrawables[recordType.ordinal()]);//枚举
        if(recordType== RecordEntity.RECORD_TYPE.CONTACT){
            ContactEntity contactEntity = (ContactEntity) entity.getRecordContent();
            viewHolder.recordContent.setText(contactEntity.getName());
        }
        else{
            viewHolder.recordContent.setText((String)entity.getRecordContent());
        }

        int[] recordFromResIds={R.string.make,R.string.scan};
        int[] recordFromColors={context.getResources().getColor(R.color.get_way_make_txt_color),context.getResources().getColor(R.color.get_way_scan_txt_color)};
        viewHolder.recordFrom.setTextColor(recordFromColors[entity.getRecordFrom().ordinal()]);
        viewHolder.recordFrom.setText(recordFromResIds[entity.getRecordFrom().ordinal()]);

        SimpleDateFormat simpleDateFormat=new SimpleDateFormat(context.getString(R.string.record_time_formater));
        String time=simpleDateFormat.format(new Date(entity.getRecordTime()));
        viewHolder.recordTime.setText(time);


        viewHolder.deleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final CustomDialog removeRecordDialog = new CustomDialog(context,R.layout.dialog_remove_record);
                ((CustomFontTextView)removeRecordDialog.findViewById(R.id.record_content)).setText(viewHolder.recordContent.getText());
                removeRecordDialog.findViewById(R.id.confirm_btn).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        recordEntityList.remove(position);
                        notifyDataSetChanged();
                        removeRecordDialog.dismiss();
                    }
                });

                removeRecordDialog.findViewById(R.id.cancel_btn).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeRecordDialog.dismiss();
                    }
                });
                removeRecordDialog.show();
            }
        });

        return convertView;
    }



    class ViewHolder
    {
        ImageView recordTypeIcon;
        CustomFontTextView recordContent,recordFrom,recordTime;
        StateBitmapImageButton deleteBtn;
    }
}
